package com.Prot2;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.Prot2.R;
import com.Prot2.Logger.CoordinatorContinuous;
import com.Prot2.Logger.CoordinatorFile;

public class LoggerContinuousUI extends Activity{
	
	private static final String TAG = "LoggerContinuousUI";
	private Button buttonToggleLogging, buttonBackToMain;
	private TextView text_loggingActive;
	private TextView text_recognizedContext;
	private ProgressBar progress_loggingActive;
	public static Context c;
	public boolean isLogging=false;
	private Handler uiHandler;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		c=this;
		
		setContentView(R.layout.logger_continuous);
		
		buttonToggleLogging = (Button) findViewById(R.id.button_ToggleLogging);
		buttonBackToMain = (Button) findViewById(R.id.button_BackToMain);
		
		text_loggingActive = (TextView) findViewById(R.id.logger_continuous_text_logging_active);
		text_recognizedContext = (TextView) findViewById(R.id.text_RecognizedContext);
		progress_loggingActive = (ProgressBar) findViewById(R.id.logger_continuous_progress_logging_active);
		
		
		buttonToggleLogging.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				//startLogging
				if(!isLogging){
					v.getContext().startService(new Intent(v.getContext(),CoordinatorContinuous.class));
					isLogging=true;
					Log.w(TAG,"called start Logging");
					text_loggingActive.setVisibility(View.VISIBLE);
					progress_loggingActive.setVisibility(View.VISIBLE);
					
					uiHandler = new Handler();
					uiHandler.postDelayed(uiRunnable, Globals.contextToggleInterval);
					
					
					
				}
				else{
					v.getContext().stopService(new Intent(v.getContext(),CoordinatorContinuous.class));
					isLogging=false;
					text_loggingActive.setVisibility(View.INVISIBLE);
					progress_loggingActive.setVisibility(View.INVISIBLE);
					finish();
				}

			}
		});
			
		buttonBackToMain.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				//Back to Main
				v.getContext().startActivity(new Intent(v.getContext(),MainActivity.class));
				Log.w(TAG,"called back to Main");
				}	
		});	
		
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
		uiHandler.removeCallbacks(uiRunnable);
		
	}
	@Override
	public void onStop(){
		super.onStop();
		uiHandler.removeCallbacks(uiRunnable);
	}
	@Override
	public void onRestart(){
		super.onRestart();
		text_recognizedContext = (TextView) findViewById(R.id.text_RecognizedContext);
		uiHandler.postDelayed(uiRunnable, Globals.contextToggleInterval);
	}
	
	
	
	private Runnable uiRunnable = new Runnable(){

		@Override
		public void run() {
			text_recognizedContext.setText("Current context: "+Globals.currentlyDetectedLabel);
			uiHandler.postDelayed(uiRunnable, Globals.contextToggleInterval);
			
		}
		
	};
	
	

}
